package com.example.superoa.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.example.superoa.model.pojos.Position;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.PositionService;
import com.example.superoa.model.vo.DeptStaffVo;
import com.example.superoa.model.vo.FunctionVo;

@Controller
public class PositionController {
@Autowired
private PositionService ps;

@RequestMapping("ps")
@ResponseBody
public List<Position> ps() {
	return ps.findAll();
}
//新增+修改角色
@RequestMapping("save-position")
@ResponseBody
public void saveposition(Position position,int depart_id) {
	System.err.println(depart_id);
	ps.saveposition(position,depart_id);
}

//查询角色
@RequestMapping("select-position")
@ResponseBody
public JSONArray selectposition(int positionId) {
	return ps.findOne(positionId);
}

//根据角色id获取已有的权限
@RequestMapping("init-role-tree")
@ResponseBody
public List<FunctionVo> initRoleTree(int positionId){
	return ps.roleTree(positionId);
}

//根据角色id和权限id保存权限的id
@RequestMapping("save-role-tree")
@ResponseBody
public void saveRoleTree(int positionId,String funIds){
	System.err.println("funIds："+funIds);
	StringBuffer sb = new StringBuffer(funIds);
	sb.deleteCharAt(sb.length()-1);
	sb.deleteCharAt(0);
	//分割
	List<Integer> ids = new ArrayList<>();
	String[] split = sb.toString().split(",");
	for (String s : split) {
		ids.add(Integer.parseInt(s));
	}
	ps.saveGrant(ids, positionId);
}


//查询已拥有该角色的用户
@RequestMapping("select-role-staff")
@ResponseBody
public JSONArray ce(int positionId){
	return ps.findStaff(positionId);
}

//查询已拥有该角色的用户
@RequestMapping("selectall-role-staff")
@ResponseBody
public JSONArray ces(){
	return ps.findStaffAll();
}

//根据角色id和用户id解除之间的联系
@RequestMapping("select-pp")
@ResponseBody
public void cet(int staffid,int positionid){
	ps.deleteStaff(staffid, positionid);
}

//测试
@RequestMapping("ceshi")
public void cetw2(){
}

//部门--员工树状图
@RequestMapping("select-ppp")
@ResponseBody
public List<DeptStaffVo> cetw(){
	return ps.roleTree1();
}

//根据员工姓名查询员工是否存在
@RequestMapping("select-if")
@ResponseBody
public JSONArray staff(String staffname) {
	return ps.findStaffif(staffname);
}

/*//根据角色id和用户id将角色分配用户
@RequestMapping("save-role-staff")
@ResponseBody
public void saveRolestaff(int positionId,String staff){
	System.err.println(staff);
	if(!staff.equals("[]")) {
		StringBuffer sb = new StringBuffer(staff);
		sb.deleteCharAt(sb.length()-1);
		sb.deleteCharAt(0);
		//分割
		List<Integer> ids = new ArrayList<>();
		String[] split = sb.toString().split(",");
		System.err.println("split");
		for (String s : split) {
			ps.saveRoleStaff(Integer.parseInt(s), positionId);
		}
	}
	
}*/

//根据角色id和用户id将角色分配用户
@RequestMapping("update-role-staff")
@ResponseBody
public void updateRolestaff(int positionId,String staff){
	if(!staff.equals("[]")) {
		StringBuffer sb = new StringBuffer(staff);
		sb.deleteCharAt(sb.length()-1);
		sb.deleteCharAt(0);
		//分割
		List<Integer> ids = new ArrayList<>();
		String[] split = sb.toString().split(",");
		for (String s : split) {
			ids.add(Integer.parseInt(s));
		}
		System.err.println("ids:"+ids);
		ps.updateRoleStaff(ids, positionId);
	}else {
		List<Integer> idse=new ArrayList<>();
		ps.updateRoleStaff(idse, positionId);
	}
	
}
}